const express= require('express');
const common = require('../../../libs/common');
const reg = require('../../../libs/reg');

let router = express.Router();
module.exports = router;

router.post('/',(req,res)=>{
    let {email,password,emailcode,rePwd} = req.body;
    // 做效验
    if(!reg.mailReg.test(email)){
        let regData={
            status:2,
            errMsg:"Please enter a valid email address"
        }
        res.send(regData)
    }else if(!reg.pwdReg.test(password)){
        let regData={
            status:2,
            errMsg:"Enter a combination of letters and numbers between 5 and 21"
        }
        res.send(regData)
    }else if(!reg.pwdReg.test(rePwd)){
        let regData={
            status:2,
            errMsg:"Enter a combination of letters and numbers between 5 and 21"
        }
        res.send(regData)
    }else if(rePwd!=password){
        let regData={
            status:2,
            errMsg:"The password and its confirm are not the same"
        }
        res.send(regData)
    }else if(!reg.emailcodeReg.test(emailcode)){
        let regData={
            status:2,
            errMsg:"Enter six digits of mailbox verification code"
        }
        res.send(regData)
    }else{
        // 判断邮箱验证码是否正确
        req.db.query(`SELECT code,last_send_time,b_use FROM send_mail WHERE mail='${email}'`, (err, data) => {
            if (err) {
                res.sendStatus(500);
            } else if(data ==0){
                let regData={
                    status:2, 
                    errMsg:"Please send verification code"
                }
                res.send(regData)
            } else {
                if(data[0].code != emailcode){
                    let regData={
                        status:2, 
                        errMsg:"Email verification code error"
                    }
                    res.send(regData)
                }else if((Number(data[0].last_send_time)+120)<=Number(common.timestamp())){
                    // 判断邮箱验证码是否过期
                    let regData={
                        status:2,
                        errMsg:"The email verification code has expired. Please resend it"
                    }
                    res.send(regData)
                }else if(data[0].b_use[0] == 1){
                    let regData={
                        status:2,
                        errMsg:"The email verification code has expired. Please resend it"
                    }
                    res.send(regData)
                }else{
                    req.db.query(`UPDATE send_mail SET b_use=1 WHERE mail='${email}'`,(err,data)=>{
                        if(err){
                            res.sendStatus(500);
                        }else{
                            req.db.query(`SELECT ID FROM user WHERE username='${email}'`, (err, data) => {
                                if(err){
                                    res.sendStatus(500);
                                }else if(data == 0){
                                    let regData={
                                        status:2,
                                        errMsg:"Email unregistered"   //   邮箱未注册
                                    }
                                    res.send(regData)
                                }else{
                                    req.db.query(`UPDATE user SET password='${common.md5(password)}' WHERE ID='${data[0].ID}'`,(err,data)=>{
                                        if(err){
                                            res.sendStatus(500);
                                        }else{
                                            let regData={
                                                status:1
                                            }
                                            res.send(regData)
                                        }
                                    })
                                }
                            })
                        }
                    })
                }
            }
        })
        
    }
})
